package com.ywm.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ywm.commonUtil.R;
import com.ywm.eduService.entity.EduCourse;
import com.ywm.eduService.entity.EduTeacher;
import com.ywm.eduService.entity.vo.CourseInfoVo;
import com.ywm.eduService.entity.vo.CoursePublishVo;
import com.ywm.eduService.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/eduService/edu-course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;


    //分页功能
    @ApiOperation(value = "课程列表分页查询")
    @GetMapping("/getCourseListByPage/{current}/{limit}")
    public R getCourseListByPage(@PathVariable long current, @PathVariable long limit) {
        //创建page对象
        Page<EduCourse> teacherPage = new Page<>(current, limit);
        //调用方法实现分页
        //调用方式时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        courseService.page(teacherPage, null);
        long total = teacherPage.getTotal();
        List<EduCourse> records = teacherPage.getRecords();//数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    //课程列表
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        System.out.println("-------------------------");
        return R.ok().data("list", list);
    }

    //添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws Exception {
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfoById(@PathVariable("courseId") String courseId) {
        CourseInfoVo courseInfo = courseService.getCourseInfoById(courseId);
        return R.ok().data("info", courseInfo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) throws Exception {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo cpv = courseService.getPublishCourseInfo(id);
        cpv.setId(id);
        System.out.println(cpv);
        return R.ok().data("cpv", cpv);
    }

    //课程最终发布
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程删除
    @ApiOperation(value = "课程删除")
    @DeleteMapping("deleteCourse/{id}")
    public R deleteCourseById(@PathVariable String id) throws Exception {
        courseService.removeCourse(id);
        return null;
    }
}


